package com.pucheng.bracelet

import android.Manifest
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.LogUtils
import com.gyf.barlibrary.ImmersionBar
import com.gyf.immersionbar.ktx.destroyImmersionBar
import com.pucheng.bracelet.app.util.NotificationUtils
import com.pucheng.bracelet.app.service.NotificationCollectorService
import com.pucheng.bracelet.app.service.SyncDataService
import com.pucheng.bracelet.app.util.ByteUtils
import kotlinx.android.synthetic.main.activity_main.*
import me.hgj.jetpackmvvm.demo.app.ext.init
import me.hgj.jetpackmvvm.demo.app.ext.initMain
import me.hgj.jetpackmvvm.demo.app.ext.interceptLongClick
import permissions.dispatcher.NeedsPermission
import permissions.dispatcher.OnNeverAskAgain
import permissions.dispatcher.OnPermissionDenied
import permissions.dispatcher.RuntimePermissions

@RuntimePermissions
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        ImmersionBar.with(this).statusBarColor(R.color.white).statusBarDarkFont(true)
        setContentView(R.layout.activity_main)
        getPermissionWithPermissionCheck()
//        NotificationUtils.areNotificationsEnabled()
//        val string: String = Settings.Secure.getString(
//            contentResolver,
//            "enabled_notification_listeners"
//        )
        if (!NotificationUtils.notificationListenerEnable()) {
            NotificationUtils.gotoNotificationAccessSetting(this@MainActivity)
        }
        //初始化viewpager2
        mainViewpager.initMain(this)
        //初始化 bottomBar
        mainBottom.init{
            when (it) {
                R.id.navigation_home -> mainViewpager.setCurrentItem(0, false)
                R.id.navigation_bracelet -> mainViewpager.setCurrentItem(1, false)
                R.id.navigation_me -> mainViewpager.setCurrentItem(2, false)
            }
        }
        mainBottom.interceptLongClick(R.id.navigation_home,R.id.navigation_bracelet,R.id.navigation_me)
//        startService(Intent(this, SyncDataService::class.java))
//        LogUtils.e("makeChecksum = "+ByteUtils.makeChecksum("今日头条\n阿萨斯啊飒飒是按时啊是啊啥就啊 啊撒 啊啊 啊是啊 啊撒 按时安安三阿萨斯啊飒飒是按时啊是啊啥就啊 啊撒 啊啊 啊是啊 啊撒 按时安安三阿萨斯啊飒飒是按时啊是啊啥就啊 啊撒 啊啊 啊是啊 啊撒 按时安安三阿萨斯啊飒飒是按时啊是啊啥就啊 啊撒 啊啊 啊是啊 啊撒 按时安安三阿萨斯啊飒飒是按时啊是啊啥就啊 啊撒 啊啊 啊是啊 啊撒 按时安安三".toByteArray()))
    }

    /**
     * 权限操作   定位权限
     * 相关操作在@NeedsPermission，@OnPermissionDenied，@OnNeverAskAgain中都有
     */
    //获取权限
    @NeedsPermission(
        Manifest.permission.ACCESS_COARSE_LOCATION,
        Manifest.permission.ACCESS_FINE_LOCATION,
        Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
    )
    fun getPermission() { //同意权限
    }

    //拒绝权限
    @OnPermissionDenied(
        Manifest.permission.ACCESS_COARSE_LOCATION,
        Manifest.permission.ACCESS_FINE_LOCATION
    )
    fun getLocationDenied() {
        onEnableLocationClicked()
    }

    fun onEnableLocationClicked() {
        val intent =
            Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
        startActivity(intent)
    }

    //不在提醒权限
    @OnNeverAskAgain(
        Manifest.permission.ACCESS_COARSE_LOCATION,
        Manifest.permission.ACCESS_FINE_LOCATION
    )
    fun externalLocationNever() {

    }

    @SuppressLint("NeedOnRequestPermissionsResult")
    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        onRequestPermissionsResult(requestCode, grantResults)
    }

//    override fun onDestroy() {
//        super.onDestroy()
//        destroyImmersionBar()
//    }

}